set dotenv-load := true
set shell := ["bash", "-euc"]
set windows-shell := ["powershell.exe", "-NoLogo", "-NoProfile", "-Command"]

package_name := "{{ cookiecutter.package_module }}"
python_min_version := "{{ cookiecutter.min_python_version }}"
python_max_version := "{{ cookiecutter.max_python_version }}"
python_dev_version := python_min_version
pypi_server_url := "{{ cookiecutter.pypi_server_url | replace('/simple/', '') | replace('/simple', '') | replace('/$', '') }}"

# Show available recipes
default:
    @just --list

# Sync workspace deps and install pre-commit hooks
init:
    uv tool install rust-just
    uv sync --all-groups
    uvx pre-commit install

# Run ruff fix + format + check
lint:
    uvx ruff check --fix .
    uvx ruff format .
    uvx ruff check .

# Add `# noqa` for current violations, then re-run lint
lint-add-noqa: && lint-pre-commit lint
    uvx ruff check --add-noqa .

# Run all pre-commit hooks against the whole tree
lint-pre-commit:
    uvx pre-commit run --all-files

# Watch and re-run ruff on changes
lint-watch:
    uvx ruff check --watch .

# Run tests with the dev Python version
test:
    @just test-version {% raw %}{{python_dev_version}}{% endraw %}

# Run tests across the configured Python version range
test-all:
    uv run python -c "import subprocess; mn=int('{% raw %}{{python_min_version}}{% endraw %}'.split('.')[1]); mx=int('{% raw %}{{python_max_version}}{% endraw %}'.split('.')[1]); [subprocess.check_call(['uv','run','--all-groups','--python',f'3.{m}','pytest','--cov={% raw %}{{package_name}}{% endraw %}','--cov-report=xml','--cov-report=term-missing','-v','packages/']) for m in range(mn, mx + 1)]"

# Run tests for a specific Python version
test-version version:
    uv run --all-groups --python {% raw %}{{version}}{% endraw %} pytest --cov={% raw %}{{package_name}}{% endraw %} --cov-report=xml --cov-report=term-missing -v packages/

# Serve docs locally
docs:
    uv run --group docs mkdocs serve

# Build static docs
docs-build:
    uv run --group docs mkdocs build

# Deploy docs to GitHub Pages
deploy-gh-pages:
    uv run --group docs mkdocs gh-deploy --force

# Build sdist + wheel for every workspace package
build:
    uv build --all-packages

# Publish to public PyPI (skip packages whose version already exists)
publish-pypi:
    uv publish --check-url https://pypi.org/simple/

# Publish to the private PyPI server (skip packages whose version already exists)
publish-pypi-server:
    uv publish --username {% raw %}{{env_var('PYPI_SERVER_USERNAME')}}{% endraw %} --password {% raw %}{{env_var('PYPI_SERVER_PASSWORD')}}{% endraw %} --publish-url {% raw %}{{env_var_or_default('PYPI_SERVER_URL', pypi_server_url)}}{% endraw %} --check-url {% raw %}{{env_var_or_default('PYPI_SERVER_URL', pypi_server_url)}}{% endraw %}/simple/

# Publish to both indexes
publish-all: publish-pypi publish-pypi-server

# Build then publish to public PyPI
deploy-pypi: build publish-pypi

# Build then publish to the private server
deploy-pypi-server: build publish-pypi-server

# Build then publish to both indexes
deploy-all: build publish-all

# Export pinned deps to requirements.txt
export-deps:
    uv export --no-hashes --output-file requirements.txt
